package cn.t.facade.user;

import cn.t.converter.user.UserInfoRequestConverter;
import cn.t.dto.user.CreateUserRequest;
import cn.t.dto.user.CustomerFirmDTO;
import cn.t.dto.user.CustomerIndividualDTO;
import cn.t.dto.user.ImportUserRequest;
import cn.t.dto.user.ParticipantInfoDTO;
import cn.t.dto.user.RoleInfoDTO;
import cn.t.dto.user.UserInfoDTO;
import cn.t.dto.user.UserPassportDTO;
import cn.t.model.user.CostCenterDO;
import cn.t.model.user.CustomerFirmDO;
import cn.t.model.user.RoleInfoDO;
import cn.t.service.user.CostCenterService;
import cn.t.service.user.CustomerFirmService;
import cn.t.service.user.RoleInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author l
 */
@Component
public class WrappedEnterpriseFacade {

    public List<String> validateImportUserRequest(String firmCode, ImportUserRequest request) {
        List<String> errors = new ArrayList<>();
        if (request == null) {
            errors.add("导入用户信息不能为空");
            return errors;
        }
        if (StringUtils.isBlank(request.getIndividualName())) {
            errors.add("导入的领料员姓名不能都为空");
        }
        if (StringUtils.isBlank(request.getCardCode()) || StringUtils.isBlank(request.getEmployeeCode())) {
            errors.add("导入的卡号或员工号不能都为空");
        }

        CustomerFirmDO customerFirmDO = customerFirmService.findByCode(firmCode);
        if (customerFirmDO == null) {
            errors.add("导入的客户不存在");
        } else {
            request.setFirmId(customerFirmDO.getId());
        }

        RoleInfoDO roleInfo = null;
        if (customerFirmDO != null && StringUtils.isNotBlank(request.getRoleName())) {
            List<RoleInfoDO> tmp = roleInfoService.getRoleInfoByNameAndFirm(request.getRoleName(), customerFirmDO.getId());
            if (CollectionUtils.isEmpty(tmp)) {
                errors.add("导入的权限组不存在");
            } else {
                roleInfo = tmp.get(0);
                request.setRoleId(roleInfo.getId());
            }

            if (!CollectionUtils.isEmpty(tmp) && tmp.size() > 1) {
                errors.add("导入的权限组重复");
            }
        }


        if (customerFirmDO != null && StringUtils.isNotBlank(request.getCostCenterName())) {
            List<CostCenterDO> tmp = costCenterService.findCostCenterByNameAndFirm(request.getCostCenterName(), customerFirmDO.getId());
            if (CollectionUtils.isEmpty(tmp)) {
                errors.add("导入的成本中心不存在");
            } else {
                CostCenterDO costCenterDO = tmp.get(0);
                request.setCostCenterId(costCenterDO.getId());
            }
            if (!CollectionUtils.isEmpty(tmp) && tmp.size() > 1) {
                errors.add("导入的成本中心组重复");
            }
        }

        return errors;
    }

    @Transactional
    public void importUserInfo(List<ImportUserRequest> excelDataList) {
        if (CollectionUtils.isEmpty(excelDataList)) {
            return;
        }

        for(ImportUserRequest excelData : excelDataList){
            CreateUserRequest request = userInfoRequestConverter.convert(excelData);
            Map<Integer, UserPassportDTO> passportMap = userInfoRequestConverter.convertPassport(request);
            UserInfoDTO userInfo = userInfoRequestConverter.convertUserInfo(request);
            CustomerIndividualDTO customerIndividual = userInfoRequestConverter.convertCustomerInfo(request);
            ParticipantInfoDTO participantInfoDTO = userInfoRequestConverter.convertParticipantInfo(request);
            CustomerFirmDTO customerFirmDO = userInfoRequestConverter.convertCustomerFirm(excelData);
            RoleInfoDTO roleInfoDTO = userInfoRequestConverter.convertRoleInfoDTO(request);

            userInfoFacade.create(passportMap, userInfo, participantInfoDTO,roleInfoDTO, customerIndividual, customerFirmDO);
        }

    }


    @Autowired
    private UserInfoFacade userInfoFacade;

    @Autowired
    private RoleInfoService roleInfoService;

    @Autowired
    private CustomerFirmService customerFirmService;

    @Autowired
    private CostCenterService costCenterService;

    @Autowired
    private UserInfoRequestConverter userInfoRequestConverter;


}
